package it.polimi.wcalendar.gui.security;

import it.polimi.wcalendar.business.security.boundary.UserManager;
import it.polimi.wcalendar.business.security.boundary.CalendarDataManager;
import it.polimi.wcalendar.business.security.entity.Event;
import it.polimi.wcalendar.business.security.entity.User;
import java.io.IOException;
import javax.ejb.Stateless;
import javax.inject.Named;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import static javax.faces.application.FacesMessage.SEVERITY_ERROR;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Inject;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultScheduleEvent;
import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleEvent;
import org.primefaces.model.ScheduleModel;

/**
 * Gestisce la pagina principale home.xhtml, in particolare lo schedule
 * calendario.
 *
 * @author unil
 */
@Named
@SessionScoped
public class CalendarBean implements Serializable {

    @EJB
    private CalendarDataManager cm;
    @EJB
    private UserManager um;

    private User user;

    private ScheduleModel eventModel;

    private Event event;

    /**
     * Carica tutti gli eventi esistenti.
     */
    @PostConstruct
    public void init() {
        event = new Event();
        user = um.getLoggedUser();
        eventModel = new DefaultScheduleModel();
        List existingEvents = cm.getAllEventsByCreator(user);
        existingEvents.addAll(cm.getAllEventsAttendTo(user));
        if (!existingEvents.isEmpty()) {
            for (Object elem : existingEvents) {
                Event event = (Event) elem;
                eventModel.addEvent(event);
            }
        }
    }

    public Date getInitialDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), Calendar.FEBRUARY, calendar.get(Calendar.DATE), 0, 0, 0);

        return calendar.getTime();
    }

    public ScheduleModel getEventModel() {
        return eventModel;
    }

    private Calendar today() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), 0, 0, 0);

        return calendar;
    }

    public ScheduleEvent getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }

    public void onEventSelect(SelectEvent selectEvent) {
        this.event = (Event) selectEvent.getObject();
    }

    public void editEvent(ActionEvent actionEvent) {
        if (event.getId() == null) {
        } else {
            List existingEvents = cm.getExistingOverlapEvents(user, event.getData(), event.getData_end());
            if(existingEvents.contains(event)) existingEvents.remove(event);
            if (event.getData().after(event.getData_end()) && !existingEvents.isEmpty()) {
                addMessage(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Errore! Evento inconsistente.", "Evento inconsistente."));
            } else {
                cm.updateEvent(event.getIde(), event.getPrivacy(), event.getInOut(), event.getDescription(), event.getData(), event.getData_end(), event.getPlace(), event.getNotificationCollection(), event.getPeopleInvitedCollection());
                eventModel.updateEvent(event);
            }
        }
        event = new Event();
    }

    private void addMessage(FacesMessage message) {
        FacesContext.getCurrentInstance().addMessage(null, message);
    }
}
